package cn.com.dhc.danlu.business.shopcar.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * @author: 吴海洋
 * @version: 1.0.0
 * @since: 2016-12-03 14:11
 * @mail: wuhaiyang@danlu.com
 * @desc: 购物车结算按钮组合控件实现
 */
public class ShopPayActionBtn extends RelativeLayout {
    private ImageView ivWarnning;
    private TextView tvPay;

    public ShopPayActionBtn(Context context) {
        this(context, null);
    }

    public ShopPayActionBtn(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShopPayActionBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.dl_shop_combination_pay, this);
        ivWarnning = (ImageView) view.findViewById(R.id.iv_warnning);
        tvPay = (TextView) view.findViewById(R.id.tv_pay);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        view.setLayoutParams(lp);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // default
        setBackgroundDrawable(createBg(true));
    }

    public boolean isCanPay() {
        return ivWarnning.getVisibility() == GONE;
    }

    public void setCanPay(boolean canPay) {
        ivWarnning.setVisibility(canPay ? GONE : VISIBLE);
        setBackgroundDrawable(canPay ? createBg(true) : createBg(false));
    }

    public StateListDrawable createBg(boolean isNormal) {
        StateListDrawable bg = new StateListDrawable();
        if (isNormal) {
            Drawable normal = getResources().getDrawable(R.color.color_b12525);
            Drawable pressed = getResources().getDrawable(R.color.color_a32222);
            bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
            bg.addState(new int[]{}, normal);
        } else {
            Drawable normal = getResources().getDrawable(R.color.color_888888);
            Drawable pressed = getResources().getDrawable(R.color.color_7a7a7a);
            bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
            bg.addState(new int[]{}, normal);
        }
        return bg;
    }

    public void setText(String string) {
        tvPay.setText(string);
    }
}